﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PursuitDrift.Interfaces;

namespace PursuitDrift.GameObjects
{
    public class DebugWriter : GameObject
    {
        private TimeSpan _elapsedTime = TimeSpan.Zero;
        private int _frameCounter;
        private int _rate;

        public DebugWriter(Game game, IPursuitDriftGame baseGame)
            : base(game, baseGame)
        {
            
        }

        public override void Update(GameTime gameTime)
        {
            _elapsedTime += gameTime.ElapsedGameTime;

            if (_elapsedTime > TimeSpan.FromSeconds(1))
            {
                _elapsedTime -= TimeSpan.FromSeconds(1);
                _rate = _frameCounter;
                _frameCounter = 0;
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (Enabled)
            {
                _frameCounter++;

                Vector2 _pos = new Vector2(10);
                Color _color;
                string _text = string.Format("FPS: {0}", _rate);

                if (_rate > 40)
                    _color = new Color(0, 255, 0);
                else if (_rate > 20)
                    _color = Color.Yellow;
                else
                    _color = Color.Red;

                BaseGame.SpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
                BaseGame.SpriteBatch.DrawString(BaseGame.DebugFont, _text, _pos, _color);
                BaseGame.SpriteBatch.End();
            }

            base.Draw(gameTime);
        }

        public void ToggleDebugView()
        {
            Enabled = !Enabled;
        }
    }
}
